package com.qcby.xmdemo.Controller;

import com.alibaba.excel.EasyExcel;
import com.qcby.xmdemo.model.User;
import com.qcby.xmdemo.service.ExcelService;
import com.qcby.xmdemo.tool.UserDataListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/excel")
public class ExcelController {

    @PostMapping("/import")
    public String importExcel(@RequestParam("file") MultipartFile file) {
        try {
            List<User> userList = EasyExcel.read(file.getInputStream(), User.class, new UserDataListener()).sheet().doReadSync();
            for (User user : userList) {
                System.out.println(user);
            }
            return "导入成功";
        } catch (Exception e) {
            e.printStackTrace();
            return "导入失败：" + e.getMessage();
        }
    }
    @Autowired
    private ExcelService excelService;
    @GetMapping("/export")
    public void exportExcel(HttpServletResponse response) {
        try {
            excelService.exportExcel(response);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
